package net.wds.study.student.dao.impl;

import net.wds.study.student.dao.StudentDao;
import net.wds.study.student.entity.Student;
import net.wds.study.student.modal.PageModal;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sidongliang on 2020/8/21.
 */
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {

    @Resource(name = "sessionFactory")
    protected SessionFactory sf;

    /**
     * 获取当前session
     *
     * @return
     */
    public Session getSession() {
        return sf.getCurrentSession();
    }

    public SessionFactory getSf() {
        return sf;
    }


    @Override
    public boolean login(String no, String pwd) {
        Query query = getSession().createQuery("select s from Student s where s.no=? and s.password=?");
        query.setParameter(0,no);
        query.setParameter(1,pwd);
        try {
            List<Student> list = query.list();
            if(list.size()>0){
                return true;
            }
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Student> getAll() {
        Query query = getSession().createQuery("select s from Student s");
        List<Student> list = query.list();
        return list;
    }

    /**
     * @return net.wds.study.student.modal.PageModal<net.wds.study.student.entity.Student>
     * @Author GongFan
     * @Description
     * @Date 2020/9/7 21:43
     * @Param [pageIndex, pageSize, name, sex]   当页起始索引   每页条数  参数：[学生姓名   学生性别]
     **/
    @Override
    public PageModal<Student> getPageByParam(int startIndex, Integer pageSize, String name, Integer sex) {
        PageModal<Student> pageModal = new PageModal<Student>();
        if(pageSize!=null&&pageSize>0){
            pageModal.setPageSize(pageSize);
        }

        List<Object> params = new ArrayList<>();
        StringBuilder hql = new StringBuilder("select s from Student s where 1=1 ");
        if(name != null && !"".equals(name.trim())){
            hql.append(" and s.name like ? ");
            params.add("%"+name.trim()+"%");
        }
        if(sex != null && (sex == 0 || sex == 1)){
            hql.append(" and s.sex = ? ");
            params.add(sex);
        }


        Query query = getSession().createQuery(hql.toString());
        for (int i=0;i<params.size();i++){
            query.setParameter(i,params.get(i));
        }
        int totalPageNum = query.list().size();//总条数
        pageModal.setTotal(totalPageNum);
        //计算为总页数
        totalPageNum = totalPageNum%pageModal.getPageSize()==0 ? totalPageNum/pageModal.getPageSize() : (totalPageNum/pageModal.getPageSize() + 1);
        pageModal.setTotalPageNum(totalPageNum);

        //添加分页信息
        query.setFirstResult(startIndex);
        query.setMaxResults(pageModal.getPageSize());
        List<Student> list = query.list();

        pageModal.setPageData(list);

        return pageModal;
    }


    @Override
    public Student findStudentById(Integer id) {
        return null;
    }

    @Override
    public List<Student> findStudentByName(String name) {
        return null;
    }

    @Override
    public boolean deleteStudentById(Integer id) {
        return false;
    }

    @Override
    public boolean deleteStudent(Student student) {
        try {
            getSession().delete(student);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean updateStudentName(String name) {
        return false;
    }

    @Override
    public Serializable addStudent(Student student) {
        return getSession().save(student);
    }
}
